package com.ruoyi.common.utils;

import com.nlf.calendar.Lunar;

import java.time.Year;
import java.util.Date;

public class ZodiacCalculator {

    // 生肖数组，按照年份循环顺序排列
    private static final String[] ZODIACS = {
            "鼠", "牛", "虎", "兔", "龙",
            "蛇", "马", "羊", "猴", "鸡",
            "狗", "猪"
    };

    private static final String[] ZODIACSCODE = {
            "rat", "cow", "tiger", "rabbit", "dragon",
            "snake", "horse", "sheep", "monkey", "chicken",
            "dog", "pig"
    };


    // 根据年份计算生肖
    public  String getZodiac(int year) {
        // 以1900年为基准
        int baseYear = 1900;
        // 计算对应的生肖索引
        int index = (year - baseYear) % 12;
        // 返回生肖
        return ZODIACS[index];
    }
    // 根据年份计算生肖代码
    public  String getZodiacCode(int year) {
        // 以1900年为基准
        int baseYear = 1900;
        // 计算对应的生肖索引
        int index = (year - baseYear) % 12;
        // 返回生肖
        return ZODIACSCODE[index];
    }



    // 根据年龄获取生肖
    public  String getZodiacByAge(int age) {
        Lunar d = Lunar.fromDate(new Date());
        int currentYear = d.getYear() ; // 获取当前农历年份
        int birthYear = currentYear - age + 1; // 计算出生年份
        return getZodiac(birthYear); // 获取并返回生肖
    }

    // 根据年龄获取生肖代码
    public  String getZodiacCodeByAge(int age) {
        Lunar d = Lunar.fromDate(new Date());
        int currentYear = d.getYear() ; // 获取当前农历年份
        int birthYear = currentYear - age + 1; // 计算出生年份
        return getZodiacCode(birthYear); // 获取并返回生肖
    }

}
